#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n,m,ci=0;
        while (scanf("%d%d",&n,&m)!=EOF)
        {
            ci++;
            if (n==0 && m==0)break;
            int a,b,count=0;
            for (a=1;a<n-1;a++)
                for (b=a+1;b<n;b++)
                    if ((a*a+b*b+m)%(a*b)==0)count++;
            printf("Case %d: %d\n",ci,count);
        }
        if (t!=0)printf("\n");
    }
    return 0;
}
